﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Xml.Linq;

namespace Sofire.Data.XSql
{
    internal class XSqlIfNode : XSqlTestNodeBase, IXSqlIfNode
    {
        public const string NodeName = "if";

        public override XSqlType Type
        {
            get { return XSqlType.If; }
        }

        public override XObject CreateXObject()
        {
            return CreateXTest(NodeName);
        }

        public override string Compile(XSqlParameterCollection parameters)
        {
            var r = this.CompileTest(parameters);

            if(r) return this.Children.Compile(parameters);

            var next = this.Next;
            if(next != null && next.Type == XSqlType.Else)
            {
                return this.Next.Children.Compile(parameters);
            }
            return null;
        }
    }

    internal class XSqlElseNode : XSqlNodeBase, IXSqlElseNode
    {
        public const string NodeName = "else";

        public override XSqlType Type
        {
            get { return XSqlType.Else; }
        }

        public override XObject CreateXObject()
        {
            XElement element = new XElement(NodeName);
            this.CreateXChildren(element);
            return element;
        }
        protected override void Load(XmlNode node) { }

        public override string Compile(XSqlParameterCollection parameters)
        {
            return null;
        }
    }
}
